/*
 * This is the first Lab. - The lab is a Main JFrame with a Main JPanel with 3 nested JPanels
 *  - Sort of emulates a Desktop environment with a browser look and feel that allows the user to change tabs and the color of tabs thorugh the tabs themselves or the menu. 
 *  	all actions are displayed to the user through the StatusBar located at the bottom of the screen.
 *  
 *  *FUNCTIONALITY NOTES* 
 *  		-  If the action does not change anything in the window, the StatusBar will read "StatusBar" 
 *  		-  If the tab does not have focus in the JTabbedPane and the user selects to change its color through the sub-menu, 
 *  			the focus will be forced on the tab which is being adapted
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Lab {

	private JFrame frame;						//Main JFrame
	private JPanel mainPanel;					//Main JPanel which all others are nested inside
	private JPanel panel1;						//Tab 1 JPanel
	private JPanel panel2;						//Tab 2 JPanel
	private JPanel statusBarPanel;				//StatusBar JPanel
	private JRadioButton b1;					//1st RadioButton for Tab 1
	private JRadioButton b2;					//2nd RadioButton for Tab 1
	private JRadioButton b3;					//1st RadioButton for Tab 2
	private JRadioButton b4;					//2nd RadioButton for Tab 2
	private JLabel statusBar;					//JLabel - StatusBar (text)
	private JTabbedPane pane;					//JTabbedPane for tabbed window
	private JMenuBar menuBar; 					//JMenuBar for the menuBar
	private JMenu menu1; 						//JMenu component (Selecting tabs)
	private JMenu menu2;						//JMenu component (Contain Sub-Menus for changing tab colors
	private JMenu subMenu1;						//JMenu (Sub-Menu) for Tab 1
	private JMenu subMenu2;						//JMenu (Sub-Menu) for Tab 2
	private JMenuItem item1;					//JMenuItem (inside menu1 - "Tab 1")
	private JMenuItem item2;					//JMenuItem (inside menu2 - "Tab 2")
	private JMenuItem subMenuItemTab1_Black;	//JMenuItem (inside subMenu1 - "Black")
	private JMenuItem subMenuItemTab1_White;	//JMenuItem (inside subMenu1 - "Default")
	private JMenuItem subMenuItemTab2_Blue;		//JMenuItem (inside subMenu1 - "Blue")
	private JMenuItem subMenuItemTab2_DG;		//JMenuItem (inside subMenu2 - "Default")
	
	 
	public static void main (String[] args)
		{
		Lab l1 = new Lab ();	
		
		l1.createAndShowGUI();
		}
	
	private void createAndShowGUI()
		{
		/*
		 * Create all Components
		 */		
		//Frame - the main window, consists of a Panel containing all the other components
		frame = new JFrame ("Dustin Peirolo Lab1 Frame");
		
		//Main Panel - Uses the layout functionality 
		mainPanel = new JPanel(new BorderLayout());
		
		//MenuBar/Menu/MenuItem - Create the components necessary to have a menu
		menuBar     	      = new JMenuBar();
		menu1     		      = new JMenu("Tab Selection");
		menu2 		 	      = new JMenu("Tab Colors");
		subMenu1	 	      = new JMenu("Tab 1");
		subMenu2	  	      = new JMenu("Tab 2");
		item1    	  	      = new JMenuItem("Tab 1");
		item2 	   	  	  	  = new JMenuItem("Tab 2");
		subMenuItemTab1_Black = new JMenuItem("Black");
		subMenuItemTab1_White = new JMenuItem("Default");
		subMenuItemTab2_Blue  = new JMenuItem("Blue");
		subMenuItemTab2_DG	  = new JMenuItem("Default");
		
		//TabbedPane and Panel components
		pane   = new JTabbedPane();
		panel1 = new JPanel();
		panel2 = new JPanel();
		
		//StatusBar JPanel component
		statusBarPanel = new JPanel();
		
		//StatusBar JLabel Component
		statusBar = new JLabel("StatusBar");

		//Radio Button
		ButtonGroup buttonGrp  = new ButtonGroup();
		ButtonGroup buttonGrp2 = new ButtonGroup(); 
		b1 = new JRadioButton("Black");
		b2 = new JRadioButton("Default", true);
		b3 = new JRadioButton("Blue");
		b4 = new JRadioButton("Default", true);
		
		
		/*
		 * Color Tabbed JPanels (Default)
		 */
		panel1.setBackground(Color.WHITE);
		panel2.setBackground(Color.DARK_GRAY);
		
		
		/*
		 * Configure all Components
		 */
		//Add the Menu components to the MenuBar
		menuBar.add(menu1);
		menuBar.add(menu2);
		
		//Add the MenuItems to the Menu
		menu1.add(item1);
		menu1.addSeparator();
		menu1.add(item2);
		
		//Add the Menu to the Menu (Submenu)
		menu2.add(subMenu1);
		menu2.addSeparator();
		menu2.add(subMenu2);
		
		//Add the SubMenuItems to the SubMenu
		subMenu1.add(subMenuItemTab1_Black);
		subMenu1.add(subMenuItemTab1_White);
		subMenu2.add(subMenuItemTab2_Blue);
		subMenu2.add(subMenuItemTab2_DG);
		
		//Add JLabel to JPanel
		statusBarPanel.add(statusBar);
		
		//Add the Panels to the Pane
		pane.add(panel1, "Tab 1");
		pane.add(panel2, "Tab 2");
		
		//Add the buttons to the button groups so that only one can be selected at a time
		buttonGrp.add(b1);
		buttonGrp.add(b2);
		buttonGrp2.add(b3);
		buttonGrp2.add(b4);

		//Add the Menu, Pane, and Bar to the main Panel utilizing the BorderLayout Functionality	
		mainPanel.add(pane, BorderLayout.CENTER);
		mainPanel.add(menuBar, BorderLayout.NORTH);
		mainPanel.add(statusBarPanel, BorderLayout.SOUTH);

		//Add Buttons to Panels
		panel1.add(b1);
		panel1.add(b2);
		panel2.add(b3);
		panel2.add(b4);
		
		//Add the panel to the frame		
		frame.getContentPane().add(mainPanel);
		
		//Set up the Frame
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(600, 600);
		frame.setVisible(true);
		
		//Call the functionality of the interface
		panel(); 
		}

	//Method that controls what the buttons do (Action Listeners
	public void panel() 
		{
		/*
		 * Methods to implement buttons 
		 *  - Change background color
		 *  - Update statusbar
		 */
		b1.addActionListener(new ActionListener()
			{
			public void actionPerformed(ActionEvent e) 
				{
				if (panel1.getBackground() == Color.BLACK)
				{
					statusBar.setText("StatusBar");
					return;
				}
				else
					panel1.setBackground(Color.BLACK);
					statusBar.setText("You changed the background color to Black");	
				}
			});
		
		b2.addActionListener(new ActionListener()
			{
			public void actionPerformed(ActionEvent e)
				{
				if(panel1.getBackground() == Color.WHITE)
					{
					statusBar.setText("StatusBar");
					return;
					}
				else
					panel1.setBackground(Color.WHITE);
					statusBar.setText("You changed the background color to Default");
				}
			});	
		
		b3.addActionListener(new ActionListener()
			{
			public void actionPerformed(ActionEvent e) 
				{
				if (panel2.getBackground() == Color.BLUE)
					{
					statusBar.setText("StatusBar");
					return;
					}
				else
					panel2.setBackground(Color.BLUE);
					statusBar.setText("You changed the background color to Blue");	
				}
			});
	
		b4.addActionListener(new ActionListener()
			{
			public void actionPerformed(ActionEvent e)
				{
				if (panel2.getBackground() == Color.DARK_GRAY)
					{
					statusBar.setText("StatusBar");
					return;
					}
				else
					panel2.setBackground(Color.DARK_GRAY);
					statusBar.setText("You changed the background color to Default");
				}
			});
		
		/*
		 * Methods to implement menu selections
		 * Menu1
		 *  - If the selected pane is active - return
		 *  	- else select pane and update statusBar
		 */
		item1.addActionListener(new ActionListener()
			{
			public void actionPerformed(ActionEvent e)
				{
				if (pane.getSelectedIndex() == 0)
					{
					statusBar.setText("StatusBar");
					return;
					}
				else
					pane.setSelectedIndex(0);
					statusBar.setText("You Selected Tab 1");
				}
			});
		
		item2.addActionListener(new ActionListener()
			{
			public void actionPerformed(ActionEvent e)
				{
				if (pane.getSelectedIndex() == 1)
					{
					statusBar.setText("StatusBar");
					return;
					}
				else
					pane.setSelectedIndex(1);
					statusBar.setText("You Selected Tab 2");
				}
			});
		
		/*
		 * Methods to implement the submenu selections
		 */
		subMenuItemTab1_Black.addActionListener(new ActionListener()
			{
			public void actionPerformed(ActionEvent e)
				{
				if (pane.getComponentAt(0).getBackground() == Color.BLACK)
					{
					statusBar.setText("StatusBar");
					return;
					}
				else
					pane.setSelectedIndex(0);
					pane.getComponentAt(0).setBackground(Color.BLACK);
					statusBar.setText("You changed the color of Tab 1 to Black through the SubMenu");
					b1.setSelected(true);
				}
			});
		
		subMenuItemTab1_White.addActionListener(new ActionListener()
			{
			public void actionPerformed(ActionEvent e)
				{
				if (pane.getComponentAt(0).getBackground() == Color.WHITE)
					{
					statusBar.setText("StatusBar");
					return;
					}
				else
					pane.setSelectedIndex(0);
					pane.getComponentAt(0).setBackground(Color.WHITE);
					statusBar.setText("You changed the color of Tab 1 to Default through the SubMenu");
					b2.setSelected(true);
				}
			});
		
		subMenuItemTab2_Blue.addActionListener(new ActionListener()
			{
			public void actionPerformed(ActionEvent e)
				{
				if (pane.getComponentAt(1).getBackground() == Color.BLUE)
					{
					statusBar.setText("StatusBar");
					return;
					}
				else
					pane.setSelectedIndex(1);
					pane.getComponentAt(1).setBackground(Color.BLUE);
					statusBar.setText("You changed the color of Tab 2 to Blue through the SubMenu");
					b3.setSelected(true);
				}
			});
	
		subMenuItemTab2_DG.addActionListener(new ActionListener()
			{
			public void actionPerformed(ActionEvent e)
				{
				if (pane.getComponentAt(1).getBackground() == Color.DARK_GRAY)
					{
					statusBar.setText("StatusBar");
					return;
					}
				else
					pane.setSelectedIndex(1);
					pane.getComponentAt(1).setBackground(Color.DARK_GRAY);
					statusBar.setText("You changed the color of Tab 2 to Default through the SubMenu");
					b4.setSelected(true);
				}
			});
		}		
	}

